package com.atstudent.gmall.product.biz.impl;

import com.alibaba.fastjson.JSON;
import com.atstudent.gmall.common.cache.anno.GmallCache;
import com.atstudent.gmall.common.constant.GmallConstant;
import com.atstudent.gmall.product.biz.BaseCategoryBizService;
import com.atstudent.gmall.product.mapper.BaseCategory1Mapper;
import com.atstudent.gmall.product.vo.CategoryVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author 薛有朋
 * @CreatDate 2024/11/29 21:29
 **/
@Service
@Slf4j
public class BaseCategoryBizServiceImpl implements BaseCategoryBizService {

    private ConcurrentHashMap<String , List<CategoryVo>> concurrentHashMap = new ConcurrentHashMap<String , List<CategoryVo>>();

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    @GmallCache(cacheKey = GmallConstant.REDIS_CATEGORY_KEY)
    public List<CategoryVo> findAllCategoryTree() {
        return baseCategory1Mapper.findAllCategoryTree();
    }

    //使用map本地缓存
    @Override
    public List<CategoryVo> findAllLocalCategoryTree() {

        //先从本地缓存里查
        List<CategoryVo> categoryVoList = concurrentHashMap.get("allCategoryTree");
        if (categoryVoList != null && categoryVoList.size() > 0){
            log.info("从本地缓存中查到了数据");
            return categoryVoList;
        }

        //本地缓存没有  从数据库查询
        List<CategoryVo> allCategoryTree = baseCategory1Mapper.findAllCategoryTree();
        //查询出来之后 放入到本地化内存中
        concurrentHashMap.put("allCategoryTree" , allCategoryTree);
        return allCategoryTree;
    }

    //使用Redis缓存
    @Override
    public List<CategoryVo> findAllCategoryRedisTree() {
        //从redis中查询数据 判断拿到的数据是否为常量X
        String categoryTreeJSON = redisTemplate.opsForValue().get(GmallConstant.REDIS_CATEGORY_KEY);
        if (!StringUtils.isEmpty(categoryTreeJSON)){
            if (GmallConstant.REDIS_NULL_VALUE.equals(categoryTreeJSON)){
                log.info("数据库中不存在此数据，从缓存中查询到了X...........");
                return null;
            }else {
                log.info("从Redis中查询到了数据...");
                List<CategoryVo> categoryVoList = JSON.parseArray(categoryTreeJSON, CategoryVo.class);
                return categoryVoList;
            }
        }

        //redis中没有 从数据库中拿到数据
        List<CategoryVo> allCategoryTree = baseCategory1Mapper.findAllCategoryTree();
        //判断查到的数据是否为NULL   把查到的List集合转换成JSON格式存入到Redis中
        if (allCategoryTree != null && allCategoryTree.size() > 0){
            log.info("从数据库中查询到了数据...");
            String categoryVoString = JSON.toJSONString(allCategoryTree);
            redisTemplate.opsForValue().set(GmallConstant.REDIS_CATEGORY_KEY, categoryVoString);
        }else { //存入常量 X
            log.info("从数据库中查询到了数据...X");
            redisTemplate.opsForValue().set(GmallConstant.REDIS_CATEGORY_KEY, GmallConstant.REDIS_NULL_VALUE);
        }

        return allCategoryTree;
    }


}
